package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.AccountTypeDao;
import com.our.sys.project1.entity.AccountType;

@Service
@Transactional
public class AccountTypeService {

    private static Logger logger = LoggerFactory.getLogger(AccountTypeService.class);

    @Autowired
    private AccountTypeDao accountTypeDao;

    @Autowired
    private HzService hzService;

    @Transactional(readOnly = true)
    public Page<AccountType> query(Page<AccountType> page, List<PropertyFilter> filters) {
        Page<AccountType> curPage = accountTypeDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public AccountType queryById(Long id) {
        return accountTypeDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = accountTypeDao.findUnique("select count(a) from AccountType a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void save(AccountType accountType) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(accountType.getName());
            accountType.setPy(map.get("py"));
            accountType.setWb(map.get("wb"));
            if (null != accountType.getId()) {
                AccountType getAccountType = this.queryById(accountType.getId());

                if (queryCountByName(getAccountType.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个结算方式名称！");
                }

                getAccountType.setName(accountType.getName());
                getAccountType.setPy(accountType.getPy());
                getAccountType.setWb(accountType.getWb());
                getAccountType.setState(accountType.getState());
                accountTypeDao.save(getAccountType);
            } else {

                if (queryCountByName(accountType.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个结算方式名称！");
                }
                accountTypeDao.save(accountType);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            accountTypeDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
